In this project, you'll create emergency lights and sounds, just like the ones used by police cars, ambulances, and fire trucks in the UK!
On Windows:
On A CoderDojo Laptop (running Raspberry Pi OS):
sudo apt update && sudo apt install thonny
Let’s build the circuit step by step. If you’ve never connected an LED before, don’t worry! Follow these instructions carefully.
An LED has two legs. The longer leg is called the positive leg, or the anode, and it connects to the power (in our case, a GPIO pin on the Raspberry Pi Pico). The shorter leg is called the negative leg, or the cathode, and it needs to connect to GND (ground).
Here’s how to connect the LED:
The buzzer will create the sound of a siren. It also has a positive and negative side. Usually, the positive side is marked with a plus sign (+). Here’s how to connect it:
Finally, let’s add a button to control when the lights and sounds are activated. The button will act as a switch, letting us start the lights and siren by pressing it. Here’s how to connect it:
Let’s start by writing some code in Thonny to control the LED. We’re going to add a little bit of code at a time, so that you can understand how each part works. Don’t worry, we’ll explain everything!
Comments are lines in your code that the computer ignores. We use comments to explain what the code does, which is really helpful for you (and others) when reading through your program later.
Comments in Python start with the #
symbol. Anything on the same line after this symbol is ignored by the computer. For example:
# This is a comment, the computer skips this line
Let’s write the code to set up the LED. Type this into Thonny:
from machine import Pin # Import Pin to control the GPIO pins
# Set up the LED on GP15 as an output
led = Pin(15, Pin.OUT)
Explanation:
Pin
. This lets us control the Raspberry Pi Pico’s GPIO pins.Now, let’s add a function to make the LED blink on and off. Type this into Thonny:
from time import sleep # Import the sleep function to create delays
# Function to make the LED blink
def blink_led():
led.on() # Turn the LED on
sleep(0.5) # Wait for half a second
led.off() # Turn the LED off
sleep(0.5) # Wait for half a second
Explanation:
sleep
from the time
module so that we can pause the program for a short time (this creates the blink effect).blink_led()
function turns the LED on, waits for half a second, turns it off, and waits again.We want the LED to blink continuously, not just once. So, let’s add a loop to repeat the blinking. Type this into Thonny:
# Main loop to keep the LED blinking
while True:
blink_led() # Call the blink_led function to make the LED blink
Explanation:
while True:
line means "keep doing this forever."blink_led()
function to make the LED blink repeatedly.Now let’s add code to control the buzzer and make a siren sound. Add this to your code:
# Set up the buzzer on GP14 as an output
buzzer = Pin(14, Pin.OUT)
# Function to make the buzzer sound
def sound_siren():
for i in range(5): # Repeat 5 times
buzzer.on() # Turn the buzzer on
sleep(0.2) # Wait for 0.2 seconds
buzzer.off() # Turn the buzzer off
sleep(0.2) # Wait for 0.2 seconds
Explanation:
sound_siren()
function turns the buzzer on and off in quick bursts to make a siren sound.Here’s what your final code should look like:
from machine import Pin
from time import sleep
# Set up the LED and buzzer
led = Pin(15, Pin.OUT)
buzzer = Pin(14, Pin.OUT)
# Function to make the LED blink
def blink_led():
led.on()
sleep(0.5)
led.off()
sleep(0.5)
# Function to make the buzzer sound
def sound_siren():
for i in range(5):
buzzer.on()
sleep(0.2)
buzzer.off()
sleep(0.2)
# Main loop to run both the LED and the buzzer
while True:
blink_led()
sound_siren()